// 认证相关API

/**
 * 模拟后端手机号登录
 * @param {string} phoneNumber - 手机号
 * @param {string} wxCode - 微信code（可选）
 * @returns {Promise<Object>} 用户信息
 */
export const loginByPhone = async (phoneNumber, wxCode = null) => {
  // 模拟网络延迟
  await new Promise(resolve => setTimeout(resolve, 500))
  
  // 模拟手机号对应的用户数据
  const userDatabase = {
    '138****1234': {
      id: 1001,
      nickname: '小明',
      phone: '138****1234',
      avatar: '/static/images/avatar1.png',
      gender: 1,
      birthday: '1995-05-20',
      relationshipStart: '2023-01-01'
    },
    '139****5678': {
      id: 1002,
      nickname: '小红',
      phone: '139****5678',
      avatar: '/static/images/avatar2.png',
      gender: 2,
      birthday: '1996-08-15',
      relationshipStart: '2023-01-01'
    },
    '150****9012': {
      id: 1003,
      nickname: '小张',
      phone: '150****9012',
      avatar: '/static/images/avatar1.png',
      gender: 1,
      birthday: '1994-03-12',
      relationshipStart: '2022-06-18'
    },
    '186****3456': {
      id: 1004,
      nickname: '小李',
      phone: '186****3456',
      avatar: '/static/images/avatar2.png',
      gender: 2,
      birthday: '1997-11-22',
      relationshipStart: '2022-12-25'
    }
  }
  
  // 根据手机号获取用户信息，如果没有则创建新用户
  let user = userDatabase[phoneNumber]
  if (!user) {
    user = {
      id: Date.now(),
      nickname: phoneNumber.slice(0, 3) + '****' + phoneNumber.slice(-4),
      phone: phoneNumber,
      avatar: '/static/images/avatar1.png',
      gender: Math.random() > 0.5 ? 1 : 2,
      birthday: '1995-01-01',
      relationshipStart: new Date().toISOString().split('T')[0]
    }
  }
  
  // 生成对应的伴侣信息
  const partnerMap = {
    1001: {
      id: 1002,
      nickname: '小红',
      phone: '139****5678',
      avatar: '/static/images/avatar2.png',
      gender: 2,
      birthday: '1996-08-15',
      relationshipStart: '2023-01-01'
    },
    1002: {
      id: 1001,
      nickname: '小明',
      phone: '138****1234',
      avatar: '/static/images/avatar1.png',
      gender: 1,
      birthday: '1995-05-20',
      relationshipStart: '2023-01-01'
    },
    1003: {
      id: 1004,
      nickname: '小李',
      phone: '186****3456',
      avatar: '/static/images/avatar2.png',
      gender: 2,
      birthday: '1997-11-22',
      relationshipStart: '2022-12-25'
    },
    1004: {
      id: 1003,
      nickname: '小张',
      phone: '150****9012',
      avatar: '/static/images/avatar1.png',
      gender: 1,
      birthday: '1994-03-12',
      relationshipStart: '2022-06-18'
    }
  }
  
  const partner = partnerMap[user.id] || {
    id: user.id + 1,
    nickname: '亲爱的',
    phone: '1' + (Math.floor(Math.random() * 9000000000) + 1000000000).toString(),
    avatar: user.gender === 1 ? '/static/images/avatar2.png' : '/static/images/avatar1.png',
    gender: user.gender === 1 ? 2 : 1,
    birthday: '1995-01-01',
    relationshipStart: user.relationshipStart
  }
  
  // 计算恋爱天数
  const relationshipStart = new Date(user.relationshipStart)
  const today = new Date()
  const relationshipDays = Math.floor((today - relationshipStart) / (1000 * 60 * 60 * 24))
  
  // 生成积分数据
  const userPoints = Math.floor(Math.random() * 800) + 200
  const partnerPoints = Math.floor(Math.random() * 800) + 200
  
  return {
    success: true,
    data: {
      user,
      partner,
      token: 'token_' + Date.now() + '_' + user.id,
      userPoints,
      partnerPoints,
      relationshipDays,
      timestamp: Date.now()
    }
  }
}

/**
 * 检查手机号是否已注册
 * @param {string} phoneNumber - 手机号
 * @returns {Promise<Object>} 检查结果
 */
export const checkPhoneRegistered = async (phoneNumber) => {
  await new Promise(resolve => setTimeout(resolve, 200))
  
  const registeredPhones = ['138****1234', '139****5678', '150****9012', '186****3456']
  
  return {
    success: true,
    data: {
      registered: registeredPhones.includes(phoneNumber)
    }
  }
}

/**
 * 模拟微信小程序code换手机号
 * @param {string} code - 微信登录code
 * @returns {Promise<Object>} 手机号信息
 */
export const wxCodeToPhoneNumber = async (code) => {
  await new Promise(resolve => setTimeout(resolve, 300))
  
  // 模拟根据code返回手机号
  const codeMap = {
    'mock_code_001': '138****1234',
    'mock_code_002': '139****5678',
    'mock_code_003': '150****9012',
    'mock_code_004': '186****3456'
  }
  
  const phoneNumber = codeMap[code] || '138****' + (Math.floor(Math.random() * 9000) + 1000)
  
  return {
    success: true,
    data: {
      phoneNumber,
      code
    }
  }
}

export default {
  loginByPhone,
  checkPhoneRegistered,
  wxCodeToPhoneNumber
}